define(function(require, exports, module) {
	
	require('avalon2');
	/**参考https://segmentfault.com/a/1190000006192449*/
	var menuItemTpl='<ul class="submenu"><li ms-for="(index,el) in @tree">'+
			' <a ms-attr="{href:el.href,id:el.id}" ms-class="@parentItemClass(el)" >'+
       			'<i ms-class="el.className"></i>'+
       			'<span ms-class="{\'menu-text\':@level.index==0}" ms-text="el.name"></span>'+
       			'<b ms-if="el.children" class="arrow icon-angle-down"></b>'+
       		'</a><div ms-if="el.children" ms-html="@renderSubMenu(el)"></div></li></ul>';
	/*data:[{id:'',href:'', className:'',name:'',pid:'',children:[]}] 
	 *  #!id和href.hash相等，除非是# , href的hash用来关联获取那个js页面  */
	avalon.component('ms-menu-tree',{
	    template:menuItemTpl,
	    defaults: {
	       tree:[],
	       level:{index:0},
	       parentItemClass:function(item){
	    	   return item.children?'dropdown-toggle':'';
	       },
	       renderSubMenu:function(item){
	    	   return item.children&&item.children.length ? ('<wbr ms-widget="{is:"ms-menu-tree", $id:"tree_'+item.id+'",tree:el.children,level:{index:@level.index+1}}" />') : '';
		   }
	    }
	})
	
	
	var SidebarMenu=function($container,config,data){
		this.getUrl=Common.getUrl.bind(this);
		this.config=$.extend({},SidebarMenu.DEFAULT,config);
		this.data=data;
		this.$container=$container;
		this.$div=$('#sidebar');
			
		if(this.data)
		{
			this.init();
			return;
		}
		
		var that=this;
		$.ajax(this.getUrl('sidebarMenu'),{
			type:'POST',
			dataType:'json'
		}).done(function(data){
			that.data=data;
			that.init();
		}).fail(function(data){
			that.data=[];
			that.init();
		});
				
	};
	
	SidebarMenu.prototype={
		init:function(){
			this.vm = avalon.define({
                $id: "sidebar",
                config:this.config,
                curMenuId:'',
                data:this.data
            });
			avalon.scan(this.$div.get(0),this.vm);
			this.render();
			this.initMenuMap();
			this.config.afterInit&&this.config.afterInit();
		},
		initMenuMap:function(){//构造菜单id映射
			//<id,menuItem>
			this.menuItem={};
			var menuItem=this.menuItem;
			var menuMapFn=function(tree,pItem){
				$.map(tree,function(item){
					menuItem[item.id]=item;
					pItem&&(item.pid=pItem.id);
					if(item.children&&item.children.length>0)
						menuMapFn(item.children,item);
				});
			};
			menuMapFn(this.data);
		},
		render:function(){
			$('ul[wid=tree_]',this.$div).removeClass("submenu").addClass("nav nav-list");
			ace.handle_side_menu(jQuery);
			try{ace.settings.check('sidebar' , 'collapsed')}catch(e){}
		},
		getCurrentMenuId:function(){
			return this.vm.curMenuId;
		},
		selectMenuItem:function(itemId){
			if(itemId=='#')
				return;
			this.$div.find('.active').removeClass('active');
			this.$div.find('.open').removeClass('open');
			//切换curMenuId
			this.vm.curMenuId=itemId;
			//设置actice,open,菜单路径上的父菜单
			var menuItem=this.menuItem[itemId],res=[];
			while(menuItem)
			{
				$('#'+menuItem.id,this.$div).parent().addClass('active');
				res.push(menuItem.$model);
				menuItem=this.menuItem[menuItem.pid];
				menuItem&&$('#'+menuItem.id,this.$div).parent().addClass('open');
			}
			this.menuPath=res;
			return res;
		},
		getHashs:function(){
			//返回菜单里所有的href
			var res=[];
			var getTreeHash=function(tree){
				$.map(tree,function(item){
					res.push(item.href.substr(2));
					if(item.children&&item.children.length>0)
						getTreeHash(item.children);
				});
			};
			getTreeHash(this.data);
			return res;
		},
		urls:{
			sidebarMenu:'/portal/sidebarMenu.jhtml'
		}	
	};	
	
	SidebarMenu.DEFAULT={
			
	};
	
	return  SidebarMenu;
});